#plotting - average support workplace democracy by order
#alan yan
#2-9-22

####setup####
#clear environment
rm(list = ls())

#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       hrbrthemes,
       cjoint,
       formula.tools, 
       DeclareDesign,
       emmeans,
       lmtest,
       cregg,
       egg,
       car,
       boot,
       grid,
       gridExtra,
       ggpubr,
       Cairo)

#### FUNCTIONS ####

# ggplot theme
theme_shom_alt <- function (base_size = 11, waffle = FALSE) 
{
  ret <- theme_minimal(base_size = base_size, base_family = "Roboto Condensed"
  ) + 
    theme(plot.background = element_rect(fill = "#f5f5f2", 
                                         color = NA), 
          panel.background = element_rect(fill = "#f5f5f2",color = NA), 
          legend.background = element_rect(fill = "#f5f5f2",color = NA))
  if (waffle) {
    ret + theme(axis.text = element_blank())
  }
  else {
    ret
  }
}


#bootstrap means
boot.dv <- function(policy.condition, policy.order) {
  
  #bootstrapping means
  boot.outcome <- function(d, i){
    d2 <- d[i,]
    return(mean(d2$support))
  }
  
  rbind(boot(dt[which(dt$policy == policy.condition & dt$treatment == "control" & dt[[policy.order]] == "First"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "cost" & dt[[policy.order]] == "First"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "benefit" & dt[[policy.order]] == "First"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "both" & dt[[policy.order]] == "First"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "control" & dt[[policy.order]] == "Second"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "cost" & dt[[policy.order]] == "Second"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "benefit" & dt[[policy.order]] == "Second"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "both" & dt[[policy.order]] == "Second"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "control" & dt[[policy.order]] == "Third"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "cost" & dt[[policy.order]] == "Third"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "benefit" & dt[[policy.order]] == "Third"),], boot.outcome, R = 5000) %>% tidy,
        boot(dt[which(dt$policy == policy.condition & dt$treatment == "both" & dt[[policy.order]] == "Third"),], boot.outcome, R = 5000) %>% tidy) %>%
    cbind(c("control", "cost", "benefit", "both"),
          policy.condition,
          c("First", "First", "First", "First", "Second", "Second", "Second", "Second", "Third", "Third", "Third", "Third")) %>%
    as.data.frame() %>% 
    rename(
      exp.condition = `c(\"control\", \"cost\", \"benefit\", \"both\")`,
      policy.order = `c(\"First\", \"First\", \"First\", \"First\", \"Second\", \"Second\", \"Second\", `
    ) %>%
    mutate(
      policy.condition = case_when(
        policy.condition == "codeterm" ~ "Codetermination",
        policy.condition == "elections" ~ "Manager elections",
        policy.condition == "esops" ~ "ESOPs",
        policy.condition == "enviro" ~ "Fracking",
        policy.condition == "parent" ~ "Family leave"
      ) %>% 
        factor(.,
               c("Codetermination",
                 "Manager elections",
                 "ESOPs",
                 "Fracking",
                 "Family leave")),
      exp.condition = case_when(
        exp.condition == "control" ~ "Control",
        exp.condition == "cost" ~ "Cost",
        exp.condition == "benefit" ~ "Benefit",
        exp.condition == "both" ~ "Cost and benefit"
      ) %>%
        factor(.,
               c("Control",
                 "Cost",
                 "Benefit",
                 "Cost and benefit")),
      policy.order = factor(policy.order,
               c("First",
                 "Second",
                 "Third"))
    )
}

#load data
dt <- read_rds("02-framing-experiment/data/clean-data-stacked-pol")

#set seed
set.seed(123456789)

#### **workplace democracy policies averages Table S7 ####
#### support ####
rbind(
  boot.dv("codeterm", "policy_order.randomization.codetermination"),
  boot.dv("elections", "policy_order.randomization.elections"),
  boot.dv("esops", "policy_order.randomization.esops")
) %>%
  arrange(
    policy.condition,
    exp.condition
  ) %>%
  transmute(
    `Policy condition` = policy.condition,
    `Policy order` = policy.order,
    `Experimental condition` = exp.condition,
    `Average support` = round(statistic * 100, 2) %>% paste0("%"),
    `Standard error` = round(std.error * 100, 2) %>% paste0("%")
  ) %>%
  xtable::xtable() %>%
  print(include.rownames = FALSE)

